package cn.internship.modules.manage.controller;

import java.util.*;

import cn.internship.common.utils.Constant;
import cn.internship.modules.manage.entity.dto.TeacherSearche;
import cn.internship.modules.sys.controller.AbstractController;
import cn.internship.modules.sys.entity.SysUserEntity;
import cn.internship.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import cn.internship.modules.manage.entity.TeacherInfoEntity;
import cn.internship.modules.manage.service.TeacherInfoService;
import cn.internship.common.utils.PageUtils;
import cn.internship.common.utils.R;

import javax.annotation.Resource;


/**
 * 
 *
 * @author haha
 * @email haha@gmail.com
 * @date 2023-02-20 15:58:08
 */
@RestController
@RequestMapping("manage/teacherinfo")
public class TeacherInfoController extends AbstractController {
    @Autowired
    private TeacherInfoService teacherInfoService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    @RequiresPermissions("manage:teacherinfo:list")
//    public R list(@RequestParam Map<String, Object> params){
//        //只有超级管理员，才能查看所有管理员列表
//        if(getUserId() != Constant.SUPER_ADMIN){
//            params.put("user_id", getUserId());
//        }
//        PageUtils page = teacherInfoService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("manage:teacherinfo:list")
    public R list(TeacherSearche searche){
        //只有超级管理员，才能查看所有管理员列表
        if(getUserId() != Constant.SUPER_ADMIN){
           searche.setUserId(getUserId());
        }
        PageUtils page = teacherInfoService.listByPage(searche);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("manage:teacherinfo:info")
    public R info(@PathVariable("id") Integer id){
		TeacherInfoEntity teacherInfo = teacherInfoService.getById(id);

        return R.ok().put("teacherInfo", teacherInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("manage:teacherinfo:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TeacherInfoEntity teacherInfo){
        //默认分配学生账号
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUsername(teacherInfo.getTeacherNo());
        //默认密码
        sysUserEntity.setPassword(teacherInfo.getTeacherNo());
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(2L);
        sysUserEntity.setRoleIdList(roleIds);
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setCreateUserId(getUserId());
        sysUserEntity.setMobile(teacherInfo.getPhone());
        sysUserEntity.setStatus(1);
        sysUserService.saveUser(sysUserEntity);
        teacherInfo.setUserId(sysUserEntity.getUserId());
		teacherInfoService.save(teacherInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("manage:teacherinfo:update")
    public R update(@RequestBody TeacherInfoEntity teacherInfo){
		teacherInfoService.updateById(teacherInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("manage:teacherinfo:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer[] ids){
		teacherInfoService.removeByIds(Arrays.asList(ids));
        for (Integer id : ids) {
            TeacherInfoEntity teacherInfo = teacherInfoService.getById(id);
            sysUserService.removeById(teacherInfo.getId());
        }
        return R.ok();
    }

    @GetMapping("dropList")
    public Object dropList(){
        return R.ok().put("data",teacherInfoService.list());
    }

}
